package com.example.inteldoc.common;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;

import java.util.Random;

/**
 * inteldoc CommonUtils class
 *
 * @author xisheng
 * @date 2023/08/09
 */
public class CommonUtil {

    public final static String RANDOM_BASE = "abcdefghijklmnopq0123456789";

    public static String getId(String header){
        DateTime dateTime = new DateTime();
        String formattedDate = DateUtil.format(dateTime,DatePattern.PURE_DATETIME_PATTERN);
        String tail = RandomUtil.randomString(3);
        return String.format("%s"+formattedDate+"%s",header,tail);
    }

    public static String getIdRecord(){
        DateTime dateTime = new DateTime();
        String formattedDate = DateUtil.format(dateTime,DatePattern.PURE_DATE_PATTERN);
        String tail = RandomUtil.randomString(RANDOM_BASE,5);
        return String.format(formattedDate+"%s",tail);
    }


    /**
     * 获取文件的格式
     * @param filename
     * @return
     */
    public static String getFileFormat(String filename){
        int lastDotIndex = filename.lastIndexOf('.');
        if (lastDotIndex > 0) {
            String ext = filename.substring(lastDotIndex + 1);
            return ext.toLowerCase().trim();
        } else {
            return null;
        }
    }

    public static String renameFile (String filename,String newName){
        String ext = getFileFormat(filename);
        return newName + "." + ext;
    }

    public static boolean isImg(String ext){

        if(ext == null){
            return false;
        }

        if ("jpg".equals(ext)){
            return true;
        }

        if("png".equals(ext)){
            return true;
        }

        if("jpeg".equals(ext)){
            return true;
        }

        if("pdf".equals(ext)){
            return true;
        }

        return false;

    }

    /**
     * repeat - 通过源字符串重复生成N次组成新的字符串。
     *
     * @param src - 源字符串 例如: 空格(" "), 星号("*"), "浙江" 等等...
     * @param num - 重复生成次数
     * @return 返回已生成的重复字符串
     */
    public static String repeat(String src, int num) {
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < num; i++){
            s.append(src);
        }

        return s.toString();
    }


    /**
     *  隐藏字符串信息
     * @param str 源字符串
     * @param num 从源字符串的第 num 个字符开始隐藏
     * @return
     */
    public static String stringPrefix(String str,int num){

        if(str == null || ("").equals(str)){
            return str;
        }

        int strLen = str.length();

        if(num>=strLen){
            num = 1;
        }

        String head = str.substring(0,num);

        int fixLen = strLen - num;
        if (fixLen > 0){
            str = repeat("*",fixLen);
        }
        return head.concat(str);
    }

    /**
     * 生成制定范围内的随机数
     *
     * @param scopeMin
     * @param scoeMax
     * @return
     */
    private static int getRandInteger(int scopeMin, int scoeMax) {
        Random random = new Random();
        return (random.nextInt(scoeMax) % (scoeMax - scopeMin + 1) + scopeMin);
    }


    /**
     * 动态生成随机代理 agent
     * @return
     */
    public static String getUserAgent(){

        String agent = "Chrome/";
        StringBuffer agentBuffer = new StringBuffer(agent);
        //10-99
        String p1 = Integer.toString(getRandInteger(90,99));
        //0-99;
        String p2 = Integer.toString(getRandInteger(10,99));
        //0-9999;
        String p3 = Integer.toString(getRandInteger(0,9999));
        //10-99;
        String p4 = Integer.toString(getRandInteger(10,99));
        String pp = ".";

        agentBuffer.append(p1).append(pp).append(p2).append(pp).append(p3).append(pp).append(p4);
        String userAgent = agentBuffer.toString();

        return userAgent;
    }

    public static String testFuncStr(String str){
        str = "ESD" + str.concat(str);
        System.out.println("RES:"+str);
        return str;
    }

    public static void main(String[] args){
        String str = "ruk11023";
        testFuncStr(str);
        System.out.println(str);
    }
}
